﻿using System.IO;
using System.IO.Compression;
using System.Text;

namespace Huioo.Learning.SmartStation.HelperConsoleApp
{
    internal class Program
    {
        static void Main(string[] args)
        {
            string[] contents = new string[] { };
            //string path = @"D:\Packages\luxsanswunifiedplatform\LuxsanMesTool\Monitor";
            //string fname = "frmMonitor.cs";

            //path = @"D:\Packages\luxsanswunifiedplatform\LuxsanMesTool\ProjectManage\WorkManage";
            //fname = "MachineWorkManage.cs";
            //string[] contents = ReadFile(Path.Combine(path, fname));
            //foreach (string s in contents)
            //{
            //    Console.WriteLine(s);
            //    Console.WriteLine();
            //}
            //Console.ReadKey();

            StringBuilder sb = new();
            contents = Class1.contents;
            foreach (string s in contents)
            {
                string str = UnzipZippedText(s);
                Console.WriteLine(str);
                sb.AppendLine(str);
            }

            string dumpStr = sb.ToString().TrimStart();
            int start = dumpStr.IndexOf(Environment.NewLine);
            string fileName = Path.GetFileName(dumpStr.Substring(0, start));
            Console.WriteLine(fileName);

            Console.ReadKey();
            File.WriteAllText(Path.Combine(Environment.CurrentDirectory, fileName), 
                dumpStr.Substring(start), Encoding.UTF8);
        }

        static string[] ReadFile(string fpath)
        {
            StringBuilder sb = new StringBuilder();
            sb.AppendLine(fpath);

            List<string> contents = new List<string>();

            using (FileStream fs = new FileStream(fpath, FileMode.Open))
            {
                using (StreamReader reader = new StreamReader(fs))
                {
                    string str;
                    while ((str = reader.ReadLine()) != null)
                    {
                        sb.AppendLine(str);
                        if (sb.Length > 256 * 1024)
                        {
                            contents.Add(ZipText(sb.ToString()));
                            sb.Clear();
                        }
                    }

                    sb.AppendLine();
                    contents.Add(ZipText(sb.ToString()));
                }
            }
            return contents.ToArray();
        }

        static string ZipText(string text)
        {
            byte[] inputBytes = Encoding.UTF8.GetBytes(text);
            return ZipText(inputBytes);
        }

        static string ZipText(byte[] inputBytes)
        {
            using (MemoryStream outputStream = new MemoryStream())
            {
                using (GZipStream gs = new GZipStream(outputStream, CompressionMode.Compress))
                {
                    gs.Write(inputBytes, 0, inputBytes.Length);
                }
                byte[] outputBytes = outputStream.ToArray();
                string result = Convert.ToBase64String(outputBytes);
                return result;
            }
        }

        static string UnzipZippedText(string zippedText)
        {
            if (String.IsNullOrEmpty(zippedText)) return String.Empty;
            string unzipedText = null;
            try
            {
                byte[] buffer = Convert.FromBase64String(zippedText);
                MemoryStream ms = new MemoryStream(buffer);
                GZipStream zipStream = new GZipStream(ms, CompressionMode.Decompress);
                using (StreamReader streamReader = new StreamReader(zipStream))
                { unzipedText = streamReader.ReadToEnd(); }
            }
            catch (Exception ex) { unzipedText = String.Empty; }
            return unzipedText;
        }
    }
}




